home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gpp-1_42.lha / g++-1.42.0 / init_libs.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  5KB  |  217 lines

  1. /* Startup code needed by GCC C++ output code.  */
  2. /* Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* As a special exception, if you link this file with files
  21.    compiled with GCC to produce an executable, this does not cause
  22.    the resulting executable to be covered by the GNU General Public License.
  23.    This exception does not however invalidate any other reasons why
  24.    the executable file might be covered by the GNU General Public License.  */
  25.  
  26. /*  init_libs.c: Originally developed by James Kempf for SUN,
  27.  *               adapted and commented for FSF by Heinz Seidl (hgs@cygnus.com)
  28.  */
  29.  
  30. /* Exports:
  31.  *   void __initialize_libraries()
  32.  *   void __finalize_libraries()
  33.  *
  34.  * When there is no dynamic linked object, then _DYNAMIC = 0 and we return.
  35.  *
  36.  * In case of dynamic shared objects (libraries):
  37.  * The dynamic shared objects are already linked with the main program and 
  38.  * control was passed from `start' (in `crt0.o') to `_main' and from that
  39.  * to `__main' in `ccrt0.o'.
  40.  *
  41.  * The functions here have to do the _initialization_ and _finalization_ of
  42.  * all the dynamic shared objects.
  43.  */
  44.  
  45.  
  46. #if !defined( sun) || defined( NO_DYNAMIC_LIBS)
  47. /*
  48.  * No shared libraries
  49.  */
  50.  
  51. void __initialize_libraries() {}
  52. void __finalize_libraries() {}
  53.  
  54. #else
  55. /*
  56.  * dynamic shared libraries
  57.  */
  58.  
  59. #include <sys/types.h>
  60. #include <link.h>
  61. #include <dlfcn.h>
  62. #include <malloc.h>
  63. #include <stdio.h>
  64. #include "init_main.h"
  65.  
  66. extern struct link_dynamic _DYNAMIC;
  67.  
  68. /* we have to get the address of __C/DTOR_LIST__ of the shared objects from
  69.  * the runtime linker
  70.  */
  71. const char CTOR_LIST_NAME[] = "___CTOR_LIST__";
  72. const char DTOR_LIST_NAME[] = "___DTOR_LIST__";
  73.  
  74. extern ep_fp * __function_list_addr;
  75.  
  76. /* "Handles" to the dynamic shared objects can be obtained via the _DYNAMIC 
  77.  * structure; but since be have to do finalization in the opposite direction
  78.  * as initialization, we construct a doubly linked list of these handles.
  79.  */
  80.  
  81. struct _sll /* shared_library-descriptor-list */ {
  82.     struct _sll* next;
  83.     struct _sll* prev;
  84.     struct link_map* lmp;
  85. };
  86.  
  87. #define NULLP (struct _sll*)0
  88. #ifndef NULL
  89. #define NULL (void*)0
  90. #endif
  91.  
  92. static struct _sll* _sll_head = NULLP;
  93. static struct _sll* _sll_tail = NULLP;
  94.  
  95. static inline 
  96. void new(struct link_map* nlmp)
  97. {
  98.     struct _sll * this = (struct _sll *) malloc (sizeof (struct _sll));
  99.  
  100.     this->lmp  = nlmp;
  101.     this->next = NULLP;
  102.     this->prev = NULLP;
  103.  
  104.     if( !_sll_head ) {
  105.     _sll_head = this;
  106.     _sll_tail = this;
  107.     } else {
  108.     this->prev = _sll_tail;
  109.     _sll_tail->next = this;
  110.     _sll_tail = this;
  111.     }
  112. }
  113.  
  114. static
  115. void build_sll()
  116. /*
  117.  *  -- Build doubly linked list of dynamic library descriptors (link_maps)
  118.  */
  119. {
  120.     struct link_dynamic_2* ldd;
  121.     struct link_map* lmp;
  122.  
  123.     ldd  = _DYNAMIC.ld_un.ld_2;
  124.  
  125.     for( lmp = ldd->ld_loaded; lmp; lmp = lmp->lm_next ) {
  126.     new (lmp);
  127.     }
  128. }
  129.  
  130. static
  131. void delete_sll()
  132. /*
  133.  *  -- Delete doubly linked list of dynamic library descriptors
  134.  */
  135. {
  136.     struct _sll * this = _sll_tail;
  137.     struct _sll * prev;
  138.  
  139.     while ( this ) {
  140.             prev = this->prev;
  141.         free ( this);
  142.         this = prev;
  143.     }
  144. }
  145.  
  146.  
  147. static
  148. void __do_dynamic_lib( struct _sll * sll_anchor, const char * fn_name,
  149.                const char * symb_name, int forward )
  150. /* 
  151.  *   -- get the address of the initialization or finalization function
  152.  *    <fn_name> in all shared objects via `dlsym()' and call them.
  153.  */
  154. {
  155.     struct _sll * sll_ptr   = sll_anchor;
  156.     void *        dobj_handle;
  157.     ep_fp         fn;
  158.  
  159.     while( sll_ptr ) {
  160.  
  161.     /* Set up the dl_object first */
  162.  
  163.     if ( (dobj_handle = dlopen( sll_ptr->lmp->lm_name, 1 )) == NULL)
  164.        fprintf( stderr, "%s\n", dlerror());
  165.  
  166.     /* Call dlsym() looking for the function symbol. */
  167.  
  168.     fn = (ep_fp)dlsym( dobj_handle, fn_name);
  169.     
  170.     __function_list_addr = (ep_fp*)dlsym( (void*)dobj_handle, symb_name);
  171.  
  172.     /* If nonnull, call the function. */
  173.  
  174.     if( fn ) fn();
  175.  
  176.     /* Increment to next list entry, depending on flag. */
  177.  
  178.     sll_ptr = ( forward ? sll_ptr->next : sll_ptr->prev);
  179.     }
  180. }
  181.     
  182. void __initialize_libraries()
  183. /* 
  184.  *  -- Initialize the dynamically linked libraries.
  185.  */
  186. {
  187.     static struct link_dynamic *DP = &_DYNAMIC;
  188.     if ( DP == 0 ) return; /* nothing to initialize */
  189.  
  190.     build_sll();
  191.  
  192.     __do_dynamic_lib( _sll_tail, INITIALIZE_MODULE_NAME, CTOR_LIST_NAME, 0);
  193.  
  194.     delete_sll();
  195. }
  196.  
  197.  
  198. void __finalize_libraries()
  199. /*
  200.  *  -- Finalize the dynamically linked libraries.
  201.  */
  202. {
  203.     static struct link_dynamic *DP = &_DYNAMIC;
  204.     if ( DP == 0 ) return; /* nothing to finalize */
  205.  
  206.     /* Have to rebuild sll list, the user may have added something */
  207.     _sll_head = _sll_tail = NULLP;
  208.  
  209.     build_sll();
  210.  
  211.     __do_dynamic_lib( _sll_head, FINALIZE_MODULE_NAME, DTOR_LIST_NAME, 1);
  212.  
  213.     delete_sll();
  214. }
  215.  
  216. #endif /* DYNAMIC_LIBS */
  217.